<?php

// Si le fichier de qui d�finie les constantes n'a pas encore �t� appel� on le charge
if (!defined('CONFIGURATION_FILE_LOADED'))
    include( $_SERVER['DOCUMENT_ROOT'] . '/'.URL_SITE.'/includes/configuration_include.php');

include( PATH_MODELS . 'create_playlist_model.php');
include( PATH_VIEWS . 'create_playlist_view.php');
include( PATH_MODELS . 'db_connexion_model.php');

$bdd = model_db_connexion();

// Si l'utilisateur est d�j� logu�
if (!isset($_SESSION['id_user'])) {
    $str = view_not_connected();
} else {
    $line = model_user_informations($bdd, $_SESSION['id_user']);

    // Si il l'identifiant n'est plus trouv� ou si l'utilisateur veut se d�connecter
    if (empty($line) || (isset($_GET['logout']) && $_GET['logout'] == 1)) {
        session_destroy();
        header('location:' . URL_ROOT . 'index.php');
        exit();
    }

    // Si tous les champs du formulaire de connexion sont remplis
    if (!empty($_POST['id_playlist'])
            && !empty($_POST['playlist_name'])
            && !empty($_POST['playlist_description'])
            && !empty($_POST['playlist_author'])
            && !empty($_POST['playlist_type'])
            && !empty($_POST['playlist_month'])) {

        $playlist = array('playlist_name' => htmlentities($_POST['name'])
            , 'playlist_author' => htmlentities($_POST['author'])
            , 'playlist_type' => htmlentities($_POST['type'])
            , 'playlist_month' => htmlentities($_POST['month'])
            , 'playlist_description' => htmlentities($_POST['description'])
        );


        // Ce bout de code ajoute une playlist et recupere son id dans la variable $id_playlist
        $id_playlist = model_insert_playlist($bdd, $playlist);

        $str = view_playlist_created($playlist);
        
    } else if (!empty($_POST['id_playlist_type'])) {
        $str = '<h1>Veuillez remplir tous les champs obligatoires</h1>';
        $str.= view_connexion_form();
    } else {
        $str = view_create_playlist();
    }
}
?>
<?php



?>
<?php

echo $str . "\n";
?>